package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
@Validated
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result addCategory(@RequestBody @Validated CategoryDTO categoryDTO){
        log.info("新增分类:{}",categoryDTO);
        categoryService.add(categoryDTO);
        return Result.success();
    }

    /**
     * 分页查询菜品分类
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分类分页查询")
    public Result<PageResult<Category>> pageCategory(@Validated CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询分类:{}",categoryPageQueryDTO);
        return Result.success(categoryService.page(categoryPageQueryDTO));
    }

    /**
     * 改变菜品分类状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启动和禁用分类")
    public Result startOrStop(@PathVariable("status") Integer status, @RequestParam("id") Long id){
        log.info("改变菜品 {} 号分类状态: {}",status,id);
        categoryService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 修改信息
     * @param categoryDTO
     * @return
     */
    @ApiOperation("修改菜品分类")
    @PutMapping
    public Result edit(@RequestBody @Validated(CategoryDTO.edit.class) CategoryDTO categoryDTO){
        log.info("修改菜品:{}",categoryDTO);
        categoryService.edit(categoryDTO);
        return Result.success();
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @ApiOperation("根据id删除分类")
    @DeleteMapping
    public Result remove(@RequestParam("id") Long id){
        log.info("删除{}号分类",id);
        categoryService.remove(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类 1菜品 2套餐
     * @param type
     * @return
     */
    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result<List<Category>> getCategories(@RequestParam(required = false,value = "type") Integer type){
        log.info("查询 {} 类型分类",type);
        List<Category> categoryList = categoryService.get(type);
        return Result.success(categoryList);
    }
}
